'use strict';
/**
 * Module dependencies.
 */
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var runSequence = require('run-sequence');

var coverageVariable = '$$cov_' + new Date().getTime() + '$$';
var istanbul = plugins.istanbul;

// Set NODE_ENV to 'test'
gulp.task('env:test', function () {
    process.env.NODE_ENV = 'test';
});


// Mocha tests task

gulp.task('pre:test', function () {
    var sources = ['./utils/*.js','./controller/*.js'];
    gulp.src(sources)
    // Covering files
        .pipe(istanbul({coverageVariable: coverageVariable,includeUntested: true}))
        // Force `require` to return covered files
        .pipe(istanbul.hookRequire());
})

gulp.task('mocha', function (done) {
    var tests = ['./test/unitTest/*.js'];  //--recursive  通配符
    var error;
    // Run the tests
    gulp.src(tests)
        .pipe(plugins.mocha({
            reporter: "mocha-jenkins-reporter",
            reporterOptions: [
                "junit_report_name=Customer Mapper Server Side Tests",
                "junit_report_path=./test/unit/report/server-report.xml",
                "junit_report_stack=1"
            ],
            timeout: 10000
        }))
        // Creating the reports after tests run
        .pipe(istanbul.writeReports({
            dir: './test/unit/coverag',  //report path
            reporters: [ 'lcov', 'json', 'text', 'text-summary', 'cobertura' ],
            coverageVariable: coverageVariable
        }))
        .pipe(istanbul.enforceThresholds({ thresholds: { global: 50 } }))
        .on('error', function (err) {
            // If an error occurs, save it
            error = err;
        })
        .on('end', function () {
            done(error);
        });
});

gulp.task('test:server', function (done) {
    runSequence('pre:test','mocha', function(){
        process.exit();
        done();
    });
});
